package com.blrs.system.service.impl;

import java.util.List;

import com.blrs.common.core.domain.entity.SysUser;
import com.blrs.common.core.domain.model.LoginUser;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.common.utils.file.FileUtils;
import com.blrs.common.utils.uuid.IdUtils;
import com.blrs.system.domain.SysUploadFile;
import com.blrs.system.mapper.SysUploadFileMapper;
import com.blrs.system.service.ISysUploadFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 附件Service业务层处理
 * 
 * @author yuxiang.chen
 * @date 2023-07-20
 */
@Service
public class SysUploadFileServiceImpl implements ISysUploadFileService
{
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;
    private static final Logger log = LoggerFactory.getLogger(SysUploadFileServiceImpl.class);
    /**
     * 查询附件
     * 
     * @param id 附件主键
     * @return 附件
     */
    @Override
    public SysUploadFile selectSysUploadFileById(String id)
    {
        return sysUploadFileMapper.selectSysUploadFileById(id);
    }

    /**
     * 查询附件列表
     * 
     * @param sysUploadFile 附件
     * @return 附件
     */
    @Override
    public List<SysUploadFile> selectSysUploadFileList(SysUploadFile sysUploadFile)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser currentUser = loginUser.getUser();
        if(!currentUser.isAdmin()){
            sysUploadFile.setDelFlag(0);
        }
        return sysUploadFileMapper.selectSysUploadFileList(sysUploadFile);
    }

    /**
     * 新增附件
     * 
     * @param sysUploadFile 附件
     * @return 结果
     */
    @Override
    public int insertSysUploadFile(SysUploadFile sysUploadFile)
    {
        if(StringUtils.isEmpty(sysUploadFile.getId())){
            sysUploadFile.setId(IdUtils.fastUUID());
        }
        sysUploadFile.setIsSyn("0");
        sysUploadFile.setCreationBy(SecurityUtils.getUsername());
        sysUploadFile.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        sysUploadFile.setOperateId(String.valueOf(SecurityUtils.getUserId()));
        sysUploadFile.setOrganizationId(SecurityUtils.getOrgId());
        return sysUploadFileMapper.insertSysUploadFile(sysUploadFile);
    }

    /**
     * 修改附件
     * 
     * @param sysUploadFile 附件
     * @return 结果
     */
    @Override
    public int updateSysUploadFile(SysUploadFile sysUploadFile)
    {
        sysUploadFile.setIsSyn("1");
        return sysUploadFileMapper.updateSysUploadFile(sysUploadFile);
    }

    /**
     * 批量删除附件
     * 
     * @param ids 需要删除的附件主键
     * @return 结果
     */
    @Override
    public int deleteSysUploadFileByIds(String[] ids)
    {
        return sysUploadFileMapper.deleteSysUploadFileByIds(ids);
    }

    /**
     * 删除附件信息
     * 
     * @param id 附件主键
     * @return 结果
     */
    @Override
    public int deleteSysUploadFileById(String id)
    {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        //超级管理员永久删除
        if(currentUser.isAdmin()){
            SysUploadFile sysUploadFile=this.selectSysUploadFileById(id);
            //删除附件
            FileUtils.deleteFile(sysUploadFile.getPhyPath()+"/"+sysUploadFile.getNewFileName());
//            boolean f2=FileUtils.deleteFile(sysUploadFile.getPhyPath().substring(1,sysUploadFile.getPhyPath().length())+"/"+sysUploadFile.getNewFileName());
//            log.info(String.valueOf(f));
//            log.info(String.valueOf(f2));
//            log.info(sysUploadFile.getPhyPath()+"/"+sysUploadFile.getNewFileName());
//            log.info(sysUploadFile.getPhyPath().substring(1,sysUploadFile.getPhyPath().length())+"/"+sysUploadFile.getNewFileName());
            return sysUploadFileMapper.deleteSysUploadFileById(id);
        }else{
            //普通用户只做物理删除
            SysUploadFile sysUploadFile=new SysUploadFile();
            sysUploadFile.setId(id);
            sysUploadFile.setDelFlag(1);
            return this.updateSysUploadFile(sysUploadFile);
        }

    }

    @Override
    public int fileRremove(String id, String type) {
//        if(!StringUtils.isEmpty(type)&&type.equals("0")){
            SysUploadFile file=this.selectSysUploadFileById(id);
            if(file==null){
                return 1;
            }
            //删除附件
            FileUtils.deleteFile(file.getPhyPath()+"/"+file.getNewFileName());
            return sysUploadFileMapper.deleteSysUploadFileById(id);
//        }else{
//            SysUploadFile file=new SysUploadFile();
//            file.setDelFlag(1);
//            file.setId(id);
//            return sysUploadFileMapper.deleteSysUploadFileById(id);
//        }

    }
}
